<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 店铺统计信息表 Table<ai_allwin_store_stats>
 */
namespace app\allwin\model;
use think\Model;

class StoreStats extends Model
{
    protected $pk     = 'id';
    protected $table  = 'ai_allwin_store_stats';
   
    /**
      * 消费收入
      * @param array $store  店铺信息
      * @param integer $uid
      * @param integer $money(元)
      * @return void
      */
    public function payment($store,float $money){
        $info = self::where(['store_id' => $store->id])->find();
        if (empty($info)) {
            $data['member_miniapp_id'] = $store->member_miniapp_id;
            $data['store_id']          = $store->id;
            $data['payment']           = $money;
            return self::create($data);
        } else {
            $info->payment = ['inc',$money];
            return $info->save();
        }
    }

    /**
     * 优惠券优惠
    * @param integer $miniapp_id
    * @param integer $uid
    * @param integer $due_money(元)
    * @return void
    */
    public function coupon($store,float $money){
        $info = self::where(['store_id' => $store->id])->find();
        if (empty($info)) {
            $data['member_miniapp_id'] = $store->member_miniapp_id;
            $data['store_id']          = $store->id;
            $data['coupon']            = $money;
            return self::insert($data);
        } else {
            $info->coupon = ['inc',$money];
            return $info->save();
        }
    }

    /**
     * 预储会员卡
    * @param integer $miniapp_id
    * @param integer $uid
    * @param integer $due_money(元)
    * @return void
    */
    public function card(int $miniapp_id, int $store_id, float $money)
    {
        $info = self::get(['store_id' => $store_id]);
        if (empty($info)) {
            $data['member_miniapp_id'] = $miniapp_id;
            $data['store_id']          = $store_id;
            $data['card']              = $money;
            return self::insert($data);
        } else {
            $info->card         = ['inc',$money];
            return $info->save();
        }
    }

    /**
     * 创客分润
    * @param integer $miniapp_id
    * @param integer $uid
    * @param integer $due_money(元)
    * @return void
    */
    public function vip(int $miniapp_id, int $store_id, float $money)
    {
        $info = self::get(['store_id' => $store_id]);
        if (empty($info)) {
            $data['member_miniapp_id'] = $miniapp_id;
            $data['store_id']          = $store_id;
            $data['vip']               = $money;
            return self::insert($data);
        } else {
            $info->vip = ['inc',$money];
            return $info->save();
        }
    }
}
